﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.UI.WebControls;
using OSharp.Core.Data;
using OSharp.Core.Data.Extensions;
using OSharp.Demo.Contracts;
using OSharp.Demo.Dtos.System;
using OSharp.Demo.Models.System;
using OSharp.Utility.Data;
using OSharp.Utility.Extensions;

namespace OSharp.Demo.Services
{

    public class SystemService : ISystemContract
    {

        public IRepository<DictType, Guid> DictTypeRepository { protected get; set; }
        public IRepository<DictItem, Guid> DictItemRepository { protected get; set; }
        public IQueryable<DictType> DictTypes
        {
            get { return DictTypeRepository.Entities; }
        }


        public OperationResult AddDictItems(params DictItemInputDto[] inputDtos)
        {
            return DictItemRepository.Insert(inputDtos,
                dto =>
                {
                    if (string.IsNullOrEmpty(dto.Code))
                    {
                        throw new Exception("编码不能为空");
                    }
                    if (DictItemRepository.CheckExists(m => m.Code == dto.Code))
                    {
                        throw new Exception("编码为“{0}”的字典已存在，不能重复添加。".FormatWith(dto.Code));
                    }
                    if (!DictTypeRepository.CheckExists(m => m.Id == dto.DictTypeId))
                    {
                        throw new Exception("类型不存在");
                    }
                }, (dto, entity) =>
                {
                    var type = DictTypeRepository.GetByKey(dto.DictTypeId);
                    entity.Type = type;
                    return entity;
                });
        }

        public OperationResult AddDictTypes(params DictTypeInputDto[] inputDtos)
        {
            return DictTypeRepository.Insert(inputDtos,
                dto =>
                {
                    if (string.IsNullOrEmpty(dto.Code))
                    {
                        throw new Exception("编码不能为空");
                    }
                    if (DictTypeRepository.CheckExists(m => m.Code == dto.Code))
                    {
                        throw new Exception("编码为“{0}”的字典已存在，不能重复添加。".FormatWith(dto.Code));
                    }
                });
        }

        public OperationResult DeleteDictItems(params Guid[] ids)
        {
            return DictItemRepository.Delete(ids);
        }

        public OperationResult DeleteDictTypes(params Guid[] ids)
        {
            return DictTypeRepository.Delete(ids, null, (entity) =>
             {
                 DictItemRepository.Delete(entity.Item);
                 return entity;
             });
        }

        public OperationResult EditDictItems(params DictItemInputDto[] inputDtos)
        {
            return DictItemRepository.Update(inputDtos,
                (dto, entity) =>
                {
                    if (string.IsNullOrEmpty(dto.Code))
                    {
                        throw new Exception("编码不能为空");
                    }

                    if (!DictTypeRepository.CheckExists(m => m.Id == dto.DictTypeId))
                    {
                        throw new Exception("类型不存在");
                    }

                    if (DictItemRepository.CheckExists(m => m.Code == dto.Code && m.Id != dto.Id))
                    {
                        throw new Exception("编码为“{0}”的字典已存在，不能重复添加。".FormatWith(dto.Name));
                    }
                },
                (dto, entity) =>
                {
                    var type = DictTypeRepository.GetByKey(dto.DictTypeId);
                    entity.Type = type;
                    return entity;
                });
        }

        public OperationResult EditDictTypes(params DictTypeInputDto[] inputDtos)
        {
            return DictTypeRepository.Update(inputDtos,
                (dto, entity) =>
                {
                    if (string.IsNullOrEmpty(dto.Code))
                    {
                        throw new Exception("编码不能为空");
                    }

                    if (DictTypeRepository.CheckExists(m => m.Code == dto.Code && m.Id != dto.Id))
                    {
                        throw new Exception("编码为“{0}”的字典已存在，不能重复添加。".FormatWith(dto.Name));
                    }
                },
                (dto, entity) =>
                {
                    //if (dto.ParentId != null && dto.ParentId != Guid.Empty)
                    //{
                    //    entity.Parent = DictTypeRepository.GetByKey(dto.ParentId.Value);
                    //}

                    return entity;
                });
        }

        public OperationResult RestoreDictTypes(params Guid[] ids)
        {
            var count = ids.Sum(guid => DictTypeRepository.Restore(guid));
            return count > 0
                ? new OperationResult(OperationResultType.Success, "字典恢复成功，前台将看到该字典")
                : new OperationResult(OperationResultType.NoChanged);
        }

        public OperationResult RecycleDictTypes(params Guid[] ids)
        {
            var count = ids.Sum(guid => DictTypeRepository.Recycle(guid));
            return count > 0
                ? new OperationResult(OperationResultType.Success, "字典回收成功，前台将看不到该字典")
                : new OperationResult(OperationResultType.NoChanged);
        }
    }
}
